package com.baoyouqun.controller.back;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baoyouqun.base.PageResponse;
import com.baoyouqun.domain.CreateCmd.InvoiceRecordCreateCmd;
import com.baoyouqun.domain.Query.InvoiceRecordPageQuery;
import com.baoyouqun.domain.UpdateCmd.InvoiceRecordUpdateCmd;
import com.baoyouqun.domain.VO.InvoiceRecordVO;
import com.baoyouqun.service.InvoiceRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/back/invoiceRecord")
@Api(tags = "发票记录表管理接口")
public class InvoiceRecordController {

    @Resource
    private InvoiceRecordService invoiceRecordService;

    @PostMapping("create")
    @ApiOperation("新增发票记录表")
    public SingleResponse<InvoiceRecordVO> create(InvoiceRecordCreateCmd cmd) {
        return invoiceRecordService.create(cmd);
    }

    @PutMapping("update")
    @ApiOperation("修改发票记录表")
    public Response update(InvoiceRecordUpdateCmd cmd) {
        return invoiceRecordService.update(cmd);
    }

    @DeleteMapping("delete")
    @ApiOperation("根据ID删除发票记录表")
    public Response delete(String id) {
        StpUtil.checkRole("R010000");
        invoiceRecordService.removeById(id);
        return Response.buildSuccess();
    }

    @GetMapping("getById")
    @ApiOperation("根据ID查询发票记录表")
    public SingleResponse<InvoiceRecordVO> selectById(String id) {
        return SingleResponse.of(invoiceRecordService.selectById(id));
    }

    @GetMapping("page")
    @ApiOperation(value = "条件筛选分页查询", notes = "支持申请发票人、手机号、开具状态筛选，默认未开具（1），每页10条")
    public PageResponse<InvoiceRecordVO> page(
            @ApiParam(value = "筛选条件（申请发票人/手机号/开具状态）", required = false)
            @ModelAttribute InvoiceRecordPageQuery query) {
        IPage<InvoiceRecordVO> pageResult = invoiceRecordService.pageVo(query);
        return PageResponse.of(
                pageResult.getRecords(),
                pageResult.getTotal(),
                pageResult.getCurrent(),
                pageResult.getSize()
        );
    }

    @PostMapping("export")
    @ApiOperation(value = "导出发票记录Excel", notes = "按当前筛选条件导出全量数据（Hutool实现，支持中文文件名）")
    public void export(
            @ApiParam(value = "筛选条件（与分页接口一致）", required = false)
            @RequestBody(required = false) InvoiceRecordPageQuery query,
            @ApiParam(value = "下载响应流（后端自动处理）", hidden = true)
            HttpServletResponse response) {
        invoiceRecordService.exportInvoiceRecords(query, response);
    }

    @PutMapping("updateStatus")
    @ApiOperation(value = "更新开具状态", notes = "设为未开具（1）或已开具（2），对应页面“设为未开具/已开具”按钮")
    public Response updateInvoiceStatus(
            @ApiParam(value = "发票记录ID", required = true, example = "123456")
            @RequestParam String id,
            @ApiParam(value = "目标状态（1-未开具，2-已开具）", required = true, example = "2")
            @RequestParam Byte status) {
        return invoiceRecordService.updateInvoiceStatus(id, status);
    }

}